package com.wad.manager.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.wad.common.exception.BussinessException;
import com.wad.entity.system.SysMenu;
import com.wad.entity.system.SysUser;
import com.wad.manager.mapper.SysMenuMapper;
import com.wad.manager.mapper.SysRoleMenuMapper;
import com.wad.manager.service.SysMenuService;
import com.wad.manager.service.SysRoleMenuService;
import com.wad.manager.utils.MenuHelper;
import com.wad.utils.AuthContextUtil;
import com.wad.vo.common.ResultCodeEnum;
import com.wad.vo.system.SysMenuVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * @author wad
 * @date 2023/12/04 9:47
 * @project e-commerce
 **/
@Service
public class SysMenuServiceImpl implements SysMenuService {
    @Resource
    private SysMenuMapper sysMenuMapper;

    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;


    @Override
    public List<SysMenu> findNodes() {
        //查询所有的菜单返回list集合
        List<SysMenu> sysMenuList=sysMenuMapper.findAll();
        //调用工具类里面的方法，把返回list集合封装成要求的数据格式
        if (CollectionUtil.isEmpty(sysMenuList)){
            return null;
        }
        return MenuHelper.buildTree(sysMenuList);
    }



    @Transactional
    @Override
    public void save(SysMenu sysMenu) {
        sysMenuMapper.save(sysMenu);

        //新添加了子菜单，把父菜单isHalf半开状态设置为1
        updateSysRoleMenu(sysMenu);
    }

    private void updateSysRoleMenu(SysMenu sysMenu) {
        //找到父菜单，设置isHalf为1
        SysMenu parentMenu=sysMenuMapper.seletcParentMenu(sysMenu.getParentId());
        if (Objects.nonNull(parentMenu)){
            //将父菜单的isHalf值改为1
            sysRoleMenuMapper.updateSysRoleMenuIsHalf(parentMenu.getId());
            //递归调用
            updateSysRoleMenu(parentMenu);
        }
    }

    @Override
    public void update(SysMenu sysMenu) {
        sysMenuMapper.update(sysMenu);
    }

    @Override
    public void deleteById(Long menuId) {
        //根据菜单id查询是否包含子菜单
        int count=sysMenuMapper.selectCountById(menuId);
        if (count>0){
            throw new BussinessException(ResultCodeEnum.NODE_ERROR);
        }
        //如果子菜单没有则直接删除
        sysMenuMapper.deleteById(menuId);
    }


    @Override
    public List<SysMenuVo> findMenuByUserId() {
        //获取当前登录的用户id
        SysUser sysUser = AuthContextUtil.get();
        Long userId = sysUser.getId();
        //根据用户id查询当前角色的菜单
        List<SysMenu> menus=sysMenuMapper.findMenusByUserId(userId);
        //封装成前端所需要的数据格式
        List<SysMenu> sysMenuTreeList=MenuHelper.buildTree(menus);
        return buildMenus(sysMenuTreeList);
    }

    private List<SysMenuVo> buildMenus(List<SysMenu> sysMenuTreeList) {

        List<SysMenuVo> sysMenuVoList=new LinkedList<>();
        for (SysMenu sysMenu : sysMenuTreeList) {
            SysMenuVo sysMenuVo=new SysMenuVo();
            sysMenuVo.setTitle(sysMenu.getTitle());
            sysMenuVo.setName(sysMenu.getComponent());
            List<SysMenu> children = sysMenu.getChildren();
            if (!CollectionUtil.isEmpty(children)){
                sysMenuVo.setChildren(buildMenus(children));
            }
            sysMenuVoList.add(sysMenuVo);
        }
        return sysMenuVoList;
    }


}
